<template>
  <div>
    <div>
      <el-card class="box-card">
        <!-- 新增角色 -->
        <el-row>
          <el-button type="primary" icon="el-icon-plus" @click="showPeoDialog">新增角色</el-button>
        </el-row>
        <el-dialog title="编辑弹层" :visible="peoDialogVisible" @close="closePeoDialog">
          <!-- 表单内容 -->
          <el-form ref="peoForm" :model="peoForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="peoForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="peoForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-button size="small" @click="closePeoDialog">取消</el-button>
            <el-button size="small" type="primary" @click="confirm">确定</el-button>
          </el-row>
        </el-dialog>
        <!-- 列表 -->
        <el-table :data="list">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240">
            <template #default="{row}">
              <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="editPeople(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="fenchDelPeople(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination layout="prev,pager,next" :total="total" :page-size="params.pagesize" :current-page="params.page" @current-change="changePage" />
        </el-row>
      </el-card>
    </div>

  </div>
</template>

<script>
import { getList, addPeople, delPeople, getPeople, editPeople } from '@/api/setting'
export default {
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      peoDialogVisible: false,
      peoForm: {
        name: '',
        description: ''

      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }

    }
  },
  mounted() {
    this.fenchGetList()
  },
  methods: {

    async fenchGetList() {
      const { total, rows } = await getList(this.params)
      this.list = rows
      this.total = total
    },
    // 打开弹框
    showPeoDialog() {
      this.peoDialogVisible = true
    },
    // 关闭弹框
    closePeoDialog() {
      this.$refs.peoForm.resetFields()
      this.peoDialogVisible = false
    },
    // 改变页数
    changePage(page) {
      this.params.page = page
      this.fenchGetList()
    },
    // 删除
    fenchDelPeople(id) {
      this.$confirm('你确认要删除此角色吗,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPeople(id)
        this.fenchGetList()
        this.$message.success('删除角色成功')
      }).catch()
    },
    // 兜底校验
    confirm() {
      this.$refs.peoForm.validate(async valid => {
        if (valid) {
          if (this.peoForm.id) {
            await editPeople(this.peoForm)
          } else {
            await addPeople(this.peoForm)
          }
          this.fenchGetList()
          this.$message.success(`${this.peoForm.id ? '编辑' : '新增'}角色成功`)
          this.peoDialogVisible = false
          this.$refs.peoForm.resetFields()
          this.peoForm = {
            name: '',
            description: ''
          }
        }
      })
    },
    // 编辑
    async editPeople(id) {
      this.peoDialogVisible = true
      const res = await getPeople(id)
      console.log(res)
      this.peoForm = res
    }
  }
}

</script>

<style>
